<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>fetch请求</title>
    <script src="../vue.js"></script>
</head>

<body>

</body>
<script>
    let obj = {
        date() {
            return {

            }
        },
        methods: {
            handleClick() {
                // fetch是基于promise实现的
                // get请求
                fetch('http://127.0.0.1:3000/users') 
                    .then(res => res.json())
                    .then(res=>{
                        console.log(res);
                    });
                //post请求
                fetch('http://localhost:3000/list',{
                    method: 'post',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        name:'gandaner',
                        age:18
                    })
                }).then(res=>res.json()).then(res=>{
                    console.log(res);
                });
                //put请求
                fetch('http://localhost:3000/list/5',{
                    method: 'put',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        name:'gandaner',
                        age:18
                    })
                }).then(res=>res.json()).then(res=>{
                    console.log(res);
                });
                //delete请求
                fetch('http://localhost:3000/list/5',{
                    method: 'delete',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        name:'gandaner',
                        age:18
                    })
                }).then(res=>res.json()).then(res=>{
                    console.log(res);
                });
            }
        },
    }
</script>

</html>